﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using Zero.DataLayer;
using Zero.POCO;
using Zero.POCO.Compagne;
using Zero.POCO.Donnees;
using Zero.Presenter;

namespace DevExpress.MailClient.Win.Forms.Campagne
{
    public partial class frmEditCompagne : DevExpress.XtraBars.Ribbon.RibbonForm, IEditCampagneView
    {
        public List<Vague> Vague { get; set; }
        public List<Annonceur> Annonceurs { get; set; }
        public List<Filter> Filters { get; set; }
        public List<PlanView> PlanTvs { get; set; }
        public List<PlanView> PlanRds { get; set; }
        public List<PlanView> PlanPresses { get; set; }

        private EditionCampagnePresenter editionCampagnePresenter;

        bool _isCampaignModified;
        readonly Compaign sourceCompaign;

        public frmEditCompagne()
        {
            InitializeComponent();

            DialogResult = DialogResult.Cancel;
        }


        public frmEditCompagne(Compaign compaign, bool newCampaign, string caption)
        {
            InitializeComponent();
           // ribbonStatusBar.Ribbon = ribbonAnnonceur;
            //BBIView.Enabled = false;
            if (newCampaign)
            {
                //    BBIView.Enabled = false;
                //   BBISupp.Enabled = false;
                //   BBINext.Enabled = false;
                // BBIPrev.Enabled = false;
            }



            editionCampagnePresenter = new EditionCampagnePresenter(this, new VagueRepository(), new AnnonceurRepository(), new FilterRepository(), new PlanTvRepository(), new PlanRdRepository(), new PlanPrRepository());
            editionCampagnePresenter.Display();

            SLUAnnonceur.Properties.DataSource = Annonceurs;


            SLUCible.Properties.DataSource = Filters;


            SLUVague.Properties.DataSource = Vague;



            /* GCTele.DataSource = PlanTvs;
             GVTv.Columns[0].Visible = false;
             GCRadio.DataSource = PlanRds;
             GVRadio.Columns[0].Visible = false;
             GCPresse.DataSource = PlanPresses;
             GVPresse.Columns[0].Visible = false;
             */




            // DictionaryHelper.InitDictionary(spellChecker1);
            DialogResult = DialogResult.Cancel;
            sourceCompaign = compaign;
            _isCampaignModified = newCampaign;


      

        }


        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            /*if(!newCampaign)
                LayoutOption.MailCollapsed = splitContainerControl1.Collapsed;*/
        }



        public Compaign SourceCompaign { get { return sourceCompaign; } }
        public bool IsCampaignModified
        {
            get { return _isCampaignModified; /*|| richEditControl.Modified;*/ }
            set
            {
                _isCampaignModified = value;
                //richEditControl.Modified = value;
            }
        }
        #region SaveMessage event
        EventHandler onSaveMessage;
        public event EventHandler SaveMessage { add { onSaveMessage += value; } remove { onSaveMessage -= value; } }
        protected internal virtual void RaiseSaveMessage()
        {
            if (onSaveMessage != null)
                onSaveMessage(this, EventArgs.Empty);
        }
        #endregion

        void edtTo_EditValueChanged(object sender, EventArgs e)
        {
            _isCampaignModified = true;
        }

        void edtSubject_EditValueChanged(object sender, EventArgs e)
        {
            _isCampaignModified = true;
        }
        void fileSaveItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            ApplyChanges();
        }
        //void frmEditMail_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
        //    if (e.KeyCode == Keys.Escape) {
        //        if (QueryClose() != DialogResult.Cancel)
        //            Close();
        //    }
        //}
        void richEditControl_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Modifiers & Keys.Control) != 0 && e.KeyCode == Keys.S)
            {
                ApplyChanges();
                e.Handled = true;
            }
        }

        void frmEditMail_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = QueryClose();
            e.Cancel = result == DialogResult.Cancel;
        }
        DialogResult QueryClose()
        {
            if (!IsCampaignModified)
                return DialogResult.Yes;

            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.SaveQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                case DialogResult.Yes:
                    ApplyChanges();
                    return DialogResult.Yes;
            }
        }
        void ApplyChanges()
        {

            if (sourceCompaign.id == default(Guid))
                sourceCompaign.id = Guid.NewGuid();

            sourceCompaign.Name = TEName.Text;
            sourceCompaign.DateDebut = DEDebut.DateTime;
            sourceCompaign.DateFin = DEFin.DateTime;


            IsCampaignModified = false;

            RaiseSaveMessage();
        }




        private void SLUAnnonceur_EditValueChanged(object sender, EventArgs e)
        {
            var key = SLUAnnonceur.EditValue;
            if (key == null) return;
            var listeMarques = Annonceurs.First(a => a.id.ToString() == key.ToString()).Marques;
            if (listeMarques.Any())
                SLUMarque.Properties.DataSource = listeMarques;
        }

        private void SLUAnnonceur_Popup(object sender, EventArgs e)
        {
            SLUAnnonceur.Properties.View.Columns[1].Visible = false;
            SLUAnnonceur.Properties.View.Columns[2].Visible = false;
           
        }

        private void SLUMarque_Popup(object sender, EventArgs e)
        {
            SLUMarque.Properties.View.Columns[2].Visible = false;
        }

        private void SLUCible_Popup(object sender, EventArgs e)
        {
            SLUCible.Properties.View.Columns[4].Visible = false;
        }

        private void SLUVague_Popu(object sender, EventArgs e)
        {
            SLUVague.Properties.View.Columns[6].Visible = false;
        }

        private void BBIAddPlanPresse_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void BBIUnlinkPlanPresse_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void BBIDeletePlanPresse_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void BBIAddPlanRadio_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void BBIUnlinkPlanRadio_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void BBIDeletePlanRadio_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void BBIAddPlanTv_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void BBIUnlink_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void BBIDelete_ItemClick(object sender, ItemClickEventArgs e)
        {

        }


        private void SLUVague_AddNewValue(object sender, XtraEditors.Controls.AddNewValueEventArgs e)
        {
            
        }

        private void SLUCible_AddNewValue(object sender, XtraEditors.Controls.AddNewValueEventArgs e)
        {

        }

        private void SLUAnnonceur_AddNewValue(object sender, XtraEditors.Controls.AddNewValueEventArgs e)
        {

            Cursor.Current = Cursors.WaitCursor;
            var form = new frmEditAnnonceur(null, true);
            //form.Load += OnEditMailFormLoad;
            //form.FormClosed += OnEditMailFormClosed;
            form.Location = new Point(Left + (Width - form.Width) / 2, Top + (Height - form.Height) / 2);
            form.ShowDialog();
            form.Activate();
            Cursor.Current = Cursors.Default;
        }

       /* void OnEditMailFormLoad(object sender, EventArgs e)
        {
            var form = sender as frmEditCampagne;
            if (form != null)
                form.SaveMessage += OnEditMailFormSaveMessage;
        }*/


        void OnEditMailFormSaveMessage(object sender, EventArgs e)
        {
            /*     frmEditCampagne form = sender as frmEditCampagne;
                 if (form == null)
                     return;

                 if (!DataHelper.Messages.Contains(form.SourceCompaign))
                     DataHelper.Messages.Add(form.SourceCompaign);*/
            //RaiseUpdateTreeViewMessages();
        }


       /* void OnEditMailFormClosed(object sender, FormClosedEventArgs e)
        {
            frmEditCampagne form = sender as frmEditCampagne;
            if (form != null)
                form.SaveMessage -= OnEditMailFormSaveMessage;
        }*/



        private void SLUMarque_AddNewValue(object sender, XtraEditors.Controls.AddNewValueEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            var form = new frmEditMarque(null, true);
           // form.Load += OnEditMailFormLoad;
           // form.FormClosed += OnEditMailFormClosed;
            form.Location = new Point(Left + (Width - form.Width) / 2, Top + (Height - form.Height) / 2);
            form.ShowDialog();
            form.Activate();
            Cursor.Current = Cursors.Default;
        }

       

        

      
    }
}